package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Dish;
import com.tjetc.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /***
     * 用户添加收藏菜品
     * @param dish
     * @return
     */
    @RequestMapping("/add")
    public JsonResult addDish(Dish dish){
        int row = dishService.insert(dish);
        JsonResult result = new JsonResult<>(0, "", row);
        return result;
    }

    /***
     * 用户删除收藏菜品
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public JsonResult deleteDish(@PathVariable("id") Integer id){
        int row = dishService.deleteById(id);
        if(row>0){
            return new JsonResult(0,"删除成功",null);
        }else{
            return new JsonResult(1,"删除失败",null);
        }
    }

    /***
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @RequestMapping("/find/{id}")
    public JsonResult findDish(@PathVariable("id")Integer id){
        Dish dish = dishService.selectById(id);
        System.out.println(dish);
        if(dish==null){
            return new JsonResult(1,"菜品不存在",dish);
        }else{
            return new JsonResult(0,"",dish);
        }
    }


    /***
     * 查询该菜品是否被该用户收藏
     * @param id
     * @param merchantId
     * @return
     */
    @RequestMapping("/query/{id}/{merchantId}")
    public JsonResult queryDish(@PathVariable("id") Integer id, @PathVariable Integer merchantId){
        List<Dish> dishes = dishService.selectByCategoryId(id,merchantId);
        if(dishes!=null){
            return new JsonResult(0,"查询成功",dishes);
        }else{
            return new JsonResult(1,"相关菜品正在研制",dishes);
        }
    }

    /***
     * 用集合批量查询菜品信息
     * @param list
     * @return
     */
    @RequestMapping("/queryByList")
    public JsonResult queryDishByList(@RequestBody List<Integer> list){
        System.out.println(list);
        List<Dish> dishes = dishService.selectByList(list);
        if(dishes!=null){
            return new JsonResult(0,"查询成功",dishes);
        }else{
            return new JsonResult(1,"相关菜品正在研制",dishes);
        }
    }

    /***
     * 查询商家热销菜品
     * @param merchantId
     * @return
     */
    @RequestMapping("/queryHot/{merchantId}")
    public JsonResult queryHot(@PathVariable Integer merchantId){
        return new JsonResult(0,"查询成功",dishService.selectHot(merchantId));
    }

}
